package com.yh.test.web.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;

/**
 * Created by uu on 2017/2/10.
 */
@Controller
@RequestMapping("/adx")
public class AdxController {

    @RequestMapping(value = "/advertiser_create", method = RequestMethod.GET)
    public void advertiserCreate () throws IOException {

        URL url = new URL("http://adx.gm825.net/dsp/api/v2/advertiser/create");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestProperty("X-Id", "45");
        conn.setRequestProperty("X-Auth-TokenDsp-", "7043f6f1e53bbf0b0b7d63dbb7404c0c");
        conn.connect();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("advertiser_id", "1");
        jsonObject.put("name", "广告主");
        jsonObject.put("logo", "http://www.qq.com");
        jsonObject.put("brand", "http://www.baidu.com");
        jsonObject.put("site", "http://www.wankna.com");
        jsonObject.put("industry_id", "122");
        jsonObject.put("business_licence", "http://www.qq.com");
        jsonObject.put("icp", "http://www.baidu.com");

        DataOutputStream out = new DataOutputStream(
                conn.getOutputStream());
        out.writeBytes(jsonObject.toString());
        out.flush();
        out.close();

        /*OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
        writer.write(jsonObject.toString());
        writer.flush();*/

        BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String lines =reader.readLine();
        System.out.print(lines);
        reader.close();

    }
}
